export function backHandler() {
    const pages = getCurrentPages()
    if (pages.length > 1) {
        return wx.navigateBack()
    }
    wx.switchTab({
        url: '/pages/index/index'
    })
}

export function toast({title, time = 1000, icon = 'none'}, ...arg) {
    if (!title) return
    wx.showToast({
        title,
        icon,
        duration: time
    })
    const [callback] = arg
    if (!!callback) {
        let timeID = setTimeout(() => {
            clearTimeout(timeID)
            timeID = null
            callback()
        }, time)
    }
}

export function wxPay(options) {
    return new Promise(resolve => {
        wx.requestPayment({
            ...options,
            success: () => {
                resolve([null, true])
            },
            fail: error => {
                resolve([error, null])
            }
        })
    })

}

export function showValidateTip(cb) {
    wx.showModal({
        title: '提示',
        content: '温馨提示，如需抖音、美团验券，请先点击“包厢列表”选择对应的“包厢”和“时长”，在最下方支付方式中选择“团购验券”即可。',
        showCancel: false,
        success: () => cb && cb()
    })
}

export function delay(time = 300) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, time)
    })
}

export function getRoomTags(tagStr) {
    if (!tagStr) return []
    let prefixStr = ''
    if (tagStr.includes('，')) {
        prefixStr = '，'
    } else if (tagStr.includes(',')) {
        prefixStr = ','
    } else {
        prefixStr = ' '
    }
    return tagStr.split(prefixStr).filter(Boolean)
}
